from collections import Iterable
from collections import Iterator
import time


class Classmate(object):
    def __init__(self):
        self.names = list()

    def add(self, name):
        self.names.append(name)

    def __iter__(self):  # 返回一个迭代器的对象(有__iter__和__next__方法)
        '''如果想要一个对象成为一个可迭代的对象，即可使用for，那么就必须实现__iter__方法'''
        return ClassIterator(self)


class ClassIterator(object):
    def __init__(self, obj):
        self.obj = obj
        self.index = 0

    def __iter__(self):
        pass

    def __next__(self):
        try:
            self.indexes = self.index
            self.index += 1
            return self.obj.names[self.indexes]
        except IndexError:
            raise StopIteration


if __name__ == '__main__':
    classmate = Classmate()
    classmate.add('AK')
    classmate.add('AKW')
    classmate.add('AKM')

    print('判断classmate是否为可迭代对象:', isinstance(classmate, Iterable))
    classmate_iterator = iter(classmate)
    print('判断classmate_iterator是否为迭代器', isinstance(classmate_iterator, Iterator))

    for name in classmate:
        print(name)
        time.sleep(1)
